<template>
	<scroll-view scroll-y="true" class="page">
		<u-navbar :placeholder="true" title="发表动态" @leftClick="$utils.back()" border>

		</u-navbar>
		<view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					标题：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-textarea autoHeight  maxlength='999'   placeholder="请输入内容" border="none" v-model="textarea" />
				</view>
			</view>
			<view style="align-items: center;" class="  borderBottom flex pb20 pt20">
				<view class="mr20 ">
					媒体内容：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<view v-if="imageUrl.length==0&&!video.url" class="mt20 upload flex" @click="openSelect">
						<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>
					</view>
					<view :class="{mt20:imageUrl.length!=0}" v-if="imageShow">
						<imageUpload :imageShow='imageShow' @changeImageList='changeImageList' :imageUrl='imageUrl'
							@close='imageShow=null' :conquer_id='conquer_id' />
					</view>
					<view :class="{mt20:video.url}" v-if="videoShow">
						<videoUpload @changeVideo='changeVideo' @close='videoShow=false' />
					</view>
				</view>
			</view>
			
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					体重/Kg：
				</view>
				<view style="text-align: right;flex: 1;">
					<text class="iconfont icon-chizi" style="margin-right: 20rpx;" @click='showWeight=true' />
					<text @click="keyboardShow=true">
						{{weight}}Kg
					</text>
				</view>
			</view>
			<u-popup :show="showSelect" :round="10" @close="showSelect=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						选择方式
					</view>
					<u-icon name="close" @click="showSelect=false"></u-icon>
				</view>
				<view style="height: 20vh;">
					<u-grid :border="false" @click="clickGrid">
						<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex">
							<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></u-icon>
							<text class="grid-text">{{baseListItem.title}}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-popup>
			<u-popup :show="showWeight" :round="10" @close="showWeight=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						体重
					</view>
					<u-icon name="close" @click="showWeight=false"></u-icon>
				</view>
				<view style="height: 24vh;" v-if="showWeight">
					<view style="text-align: center;font-size: 40rpx;color: green;margin: 20rpx 0;">
						{{weight}}Kg
					</view>
					<rulerComponent min='0' max='150' :int='false' :active='weight' @value="finishRuler" />
				</view>
			</u-popup>
			<u-keyboard :tooltip='false' ref="uKeyboard" mode="number" :show="keyboardShow" :safeAreaInsetBottom='true'
				@close='keyboardShow=false' @change='keyboardChange' @backspace='backspace' />
		</view>
		<view class="bottom">
			<u-button :disabled="disabled" @click="pulish">发表</u-button>
		</view>
	</scroll-view>
</template>

<script>
	import rulerComponent from "@/components/sapling-vue-scale/sapling-vue-scale";
	import imageUpload from '@/components/imageUpload';
	import videoUpload from '@/components/videoUpload';
	export default {
		components: {
			imageUpload,
			videoUpload,
			rulerComponent
		},
		data() {
			return {
				keyboardShow: false,
				showWeight: false,
				conquer_id: null,
				weight: 0,
				baseList: [{
						name: 'photo',
						title: '本地图片'
					},
					{
						name: 'play-right',
						title: '本地视频'
					},
					{
						name: 'chat',
						title: '素材聊天'
					},
				],
				imageUrl: [],
				video: {

				},
				textarea: null,
				showSelect: false,
				imageShow: null,
				videoShow: false,
				disabled: false,
				loading: true,
			}
		},
		onLoad(op) {
			this.conquer_id = op.id
			this.user_id = op.user_id
			this.$api.Active.Latest(this.conquer_id).then(res => {
				if (res.data.weight) {
					this.weight = res.data.weight
				}
			})
		},

		methods: {
			backspace() {
				if (this.weight.toString().length == 1) {
					this.weight = 0
					return
				}
				this.weight = this.weight.toString().substring(0, this.weight.length - 1)
			},
			keyboardChange(e) {
				if (this.weight == 0) {
					this.weight = e
					return
				}
				if (this.weight.toString().length > 5) {
					return
				}
				if (this.weight.toString().includes('.') && e == ".") {
					return
				}
				this.weight += e.toString()
				if (this.weight > 150) {
					this.weight = "150"
					uni.$u.toast('体重不能超过150')
				}
			},
			finishRuler(e) {
				this.weight = e
			},
			clickGrid(e) {
				if (e == 0) {
					this.imageShow = 'photo'
					this.showSelect = false
				} else if (e == 1) {
					this.videoShow = true
					this.showSelect = false
				} else {
					this.imageShow = 'chat'
					this.showSelect = false
				}
			},

			openSelect() {
				this.showSelect = true
			},
			changeImageList(e) {
				console.log(e)
				this.imageUrl = e
			},
			changeVideo(e) {
				console.log(e)
				this.video = e
			},
			pulish() {
				if (!this.imageUrl.every(item => item.upload) && this.imageUrl.length) {
					uni.$u.toast('正在上传中')
					return
				}
				if (!this.video.upload && this.video.url) {
					uni.$u.toast('正在上传中')
					return
				}

				if (!this.imageUrl.length && !this.video.id) {
					uni.$u.toast('必须上传图片视频')
					return
				}

				if (!this.textarea) {
					uni.$u.toast('请输入内容')
					return
				}
				if (this.textarea.length < 6) {
					uni.$u.toast('内容长度不能小于6个字符')
					return
				}
				this.Add()

			},
			Add() {
				this.disabled = true
				let obj = {
					conquer_id: this.conquer_id,
					weight: this.weight,
					title: this.textarea,
					images: this.imageUrl.map(item => {
						return {
							url: item.url,
							small_url: item.small_url,
							title: item.title
						}
					}),
					video_ids: this.video.id ? [this.video.id] : [],
				}
				this.$api.Active.Add(obj).then(res => {
					uni.$u.toast('发布成功')
					setTimeout(() => {
						if (this.user_id) {
							uni.reLaunch({
								url: "/User/detail?conquer_id=" + this.conquer_id + '&id=' + this.user_id
							})
						} else {
							uni.reLaunch({
								url: '/Conquer/Detail?id=' + this.conquer_id
							})
						}
					}, 500)
				}).catch(err => {
					this.disabled = false
					console.log(err)
					uni.$u.toast(err.data.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		height: calc(100vh - 90rpx - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - 90rpx - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.borderBottom {
		border-bottom: 1rpx solid #f1f1f1;
	}








	.flex {
		display: flex;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

.upload {
		border-radius: 8rpx;
		background-color: #f5f5f5;
		width: 100rpx;
		height: 100rpx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	::v-deep .u-border {
		border-color: #fff !important;
	}

	::v-deep .u-textarea {
		padding: 0 !important;
	}

	.selectItem {
		font-size: 30rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.selectItem:nth-child(1) {
		border-bottom: 1rpx solid #f1f1f1;
	}

	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		view:nth-child(2) {
			flex: 0.98;
		}
	}

	.bottom {
		position: fixed;
		bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		width: calc(100% - 40rpx);
	}

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
</style>